using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;

namespace AmdInterface.HttpCom
{
	public class PlmHttpUtil
	{
		private static TimeSpan lastResponseTime;

		public static void AceptCertificate()
		{
			ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors) => true;
		}

		public static HttpStatusCode? UploadXMLTransaction(string targetUrl, XmlDocument v_objXMLDoc, string method, ref XmlDocument XMLResponse, string ClientCertName = "", string contentType = "application/xml")
		{
			HttpStatusCode? httpStatusCode = null;
			HttpWebResponse httpWebResponse = null;
			Stream stream = null;
			Stream stream2 = null;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
				byte[] bytes = Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
				httpWebRequest.Method = method;
				httpWebRequest.ContentLength = bytes.Length;
				httpWebRequest.ContentType = contentType;
				httpWebRequest.Timeout = 5000;
				if (ClientCertName != "")
				{
					X509Store x509Store = new X509Store("My");
					x509Store.Open(OpenFlags.ReadOnly);
					X509Certificate2Collection x509Certificate2Collection = (X509Certificate2Collection)(httpWebRequest.ClientCertificates = x509Store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, ClientCertName, validOnly: false));
				}
				stream = httpWebRequest.GetRequestStream();
				stream.Write(bytes, 0, bytes.Length);
				stream.Close();
				httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				httpStatusCode = httpWebResponse.StatusCode;
				if (httpStatusCode == HttpStatusCode.OK)
				{
					stream2 = httpWebResponse.GetResponseStream();
					XmlTextReader xmlTextReader = new XmlTextReader(stream2);
					XmlDocument xmlDocument = new XmlDocument();
					try
					{
						xmlDocument.Load(xmlTextReader);
					}
					catch (XmlException)
					{
					}
					XMLResponse = xmlDocument;
					xmlTextReader.Close();
				}
				httpWebResponse.Close();
				stream2.Close();
			}
			catch (WebException ex2)
			{
				if (ex2.Response != null)
				{
					using WebResponse webResponse = ex2.Response;
					HttpWebResponse httpWebResponse2 = (HttpWebResponse)webResponse;
					stream2 = httpWebResponse2.GetResponseStream();
					XmlTextReader xmlTextReader = new XmlTextReader(stream2);
					XmlDocument xmlDocument2 = new XmlDocument();
					try
					{
						xmlDocument2.Load(xmlTextReader);
					}
					catch (XmlException)
					{
					}
					XMLResponse = xmlDocument2;
					xmlTextReader.Close();
					httpStatusCode = httpWebResponse2.StatusCode;
				}
				else
				{
					XMLResponse = new XmlDocument();
					XmlNode xmlNode = XMLResponse.CreateElement("Error");
					xmlNode.InnerText = ex2.Message;
					XMLResponse.AppendChild(xmlNode);
				}
				return httpStatusCode;
			}
			catch (Exception ex4)
			{
				throw new Exception(ex4.Message);
			}
			finally
			{
				XmlTextReader xmlTextReader = null;
				stream = null;
				stream2 = null;
				httpWebResponse = null;
				HttpWebRequest httpWebRequest = null;
			}
			return httpStatusCode;
		}

		public static HttpStatusCode? GetTransaction(string targetUrl, ref XmlDocument xmlResponse, string clientCertName = "")
		{
			HttpStatusCode? httpStatusCode = null;
			HttpWebResponse httpWebResponse = null;
			Stream stream = null;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
				if (clientCertName != "")
				{
					X509Store x509Store = new X509Store("My");
					x509Store.Open(OpenFlags.ReadOnly);
					X509Certificate2Collection x509Certificate2Collection = (X509Certificate2Collection)(httpWebRequest.ClientCertificates = x509Store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, clientCertName, validOnly: false));
					httpWebRequest.PreAuthenticate = true;
				}
				httpWebRequest.Timeout = 30000;
				Stopwatch stopwatch = Stopwatch.StartNew();
				httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				stopwatch.Stop();
				PlmHttpUtil.lastResponseTime = stopwatch.Elapsed;
				httpStatusCode = httpWebResponse.StatusCode;
				if (httpStatusCode == HttpStatusCode.OK)
				{
					stream = httpWebResponse.GetResponseStream();
					XmlTextReader xmlTextReader = new XmlTextReader(stream);
					XmlDocument xmlDocument = new XmlDocument();
					try
					{
						xmlDocument.Load(xmlTextReader);
					}
					catch (XmlException)
					{
					}
					xmlResponse = xmlDocument;
					xmlTextReader.Close();
				}
				httpWebResponse.Close();
				stream.Close();
			}
			catch (WebException ex2)
			{
				if (ex2.Response != null)
				{
					using WebResponse webResponse = ex2.Response;
					HttpWebResponse httpWebResponse2 = (HttpWebResponse)webResponse;
					stream = httpWebResponse2.GetResponseStream();
					XmlTextReader xmlTextReader = new XmlTextReader(stream);
					XmlDocument xmlDocument2 = new XmlDocument();
					try
					{
						xmlDocument2.Load(xmlTextReader);
					}
					catch (XmlException)
					{
					}
					xmlResponse = xmlDocument2;
					xmlTextReader.Close();
					httpStatusCode = httpWebResponse2.StatusCode;
				}
				else
				{
					xmlResponse = new XmlDocument();
					XmlNode xmlNode = xmlResponse.CreateElement("Error");
					xmlNode.InnerText = ex2.Message;
					xmlResponse.AppendChild(xmlNode);
				}
				return httpStatusCode;
			}
			catch (Exception ex4)
			{
				throw new Exception(ex4.Message);
			}
			finally
			{
				XmlTextReader xmlTextReader = null;
				stream = null;
				httpWebResponse = null;
			}
			return httpStatusCode;
		}

		public static string FormatXML(string XML)
		{
			string result = "";
			if (XML != "")
			{
				MemoryStream memoryStream = new MemoryStream();
				XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode);
				XmlDocument xmlDocument = new XmlDocument();
				try
				{
					xmlDocument.LoadXml(XML);
					xmlTextWriter.Formatting = Formatting.Indented;
					xmlDocument.WriteContentTo(xmlTextWriter);
					xmlTextWriter.Flush();
					memoryStream.Flush();
					memoryStream.Position = 0L;
					StreamReader streamReader = new StreamReader(memoryStream);
					string text = streamReader.ReadToEnd();
					result = text;
				}
				catch (XmlException)
				{
				}
				memoryStream.Close();
				xmlTextWriter.Close();
			}
			return result;
		}
	}
}
